/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.dusin.mobile.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.log4j.Logger;

/**
 *
 * @author dusin
 */
public class MobileUserList {
    static Logger logger = Logger.getLogger(MobileUserList.class);
    Map<String,Map<String,Object>> users;
    private final int maxUsers=100; 
    private int timeout=30000;    //
    private final CheckTimeout checkThread ;
    String name;
    
    public MobileUserList(String name) {
        users=new ConcurrentHashMap <String,Map<String,Object>>();
        this.name = name;
        checkThread=new CheckTimeout(name);  //暂时停用超时检查
    }      
    
    public  Map<String,Object> add(Map<String,Object> user){
        if(user==null||users.size()>maxUsers)
            return null;
        //this.removeByUser((String)user.get("username"));
        String username = (String)user.get("username");
        String sid = (UUID.randomUUID().toString()).substring(24);
        user.put("sid", sid);
        user.put("time", new Date().getTime());  //
        logger.debug(username+" is added with "+sid);
        users.put(username,user);
        return user;
    }
    
    public Map<String,Object> getUserByUsername(String username){
        
        
        return users.get(username);
    }
    
   
    
    //
    public Map<String,Object> removeByUsername(String username){
        
        Map<String,Object> user = users.get(username);
        users.remove(username);
        logger.debug(user.get("username")+" is removed with "+user.get("sid"));        
        return user;
    }
    
    public void updateTime(String username){
        Map<String,Object> user = users.get(username);
        if(user!=null)
            user.put("time", new Date().getTime());
    }  
    
    
    public void startCheckTimeout(){
       this.checkThread.start();
    }
    
    public void stopCheckTimeout(){
        this.checkThread.stopCheck();
    }
    
    public  void  checkTimeout(){
        Date now= new Date();
        logger.debug("checktimeout for mobile login "+now);
        for(Map<String,Object> user:users.values()){
            long time = (Long)user.get("time");    
            if(now.getTime()-time>timeout*1000*60){
                users.remove((String)user.get("sid"));
                logger.debug(user.get("username")+" is timeout with "+user.get("sid"));
            }
        }
    }    
    
    public List<Map<String,Object>> getUserList(){
        List<Map<String,Object>> userList = new ArrayList<Map<String,Object>>();
        Set<String> usernames = users.keySet();
        int i=1;
        for(String username:usernames){
            Map<String,Object> user = users.get(username);
            user.put("RN", i++);
            userList.add(users.get(username));
        }
        return userList;
    }
    
    class CheckTimeout extends Thread{
        String name;
        boolean stop = true;
        
        public CheckTimeout(String name){
            this.name = name;
        }
        @Override
        public void run() {
            while(!stop){
                checkTimeout();
                try {
                    Thread.sleep(60000);
                } catch (InterruptedException ex) {
                    
                }
            }
        }
        
        public void stopCheck(){
            this.stop=true;
        }
    }    
}
